
package com.android.fitnesscenter.view.server;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.ConfigProperties.AuditListResult;
import com.android.fitnesscenter.utils.ConfigProperties.RoleType;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.MapEntity;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class ServerPermissionActivity extends BaseActivity implements OnClickListener {

    @ViewInject(R.id.txt_select_user)
    private TextView txt_select_user;

    private LinearLayout llayout_select_user;
    private RelativeLayout rlayout_qx1, rlayout_qx2, rlayout_qx3, rlayout_qx4, rlayout_qx5;

    @ViewInject(R.id.chbox_qx1)
    private CheckBox chbox_qx1;

    @ViewInject(R.id.chbox_qx2)
    private CheckBox chbox_qx2;

    @ViewInject(R.id.chbox_qx3)
    private CheckBox chbox_qx3;

    @ViewInject(R.id.chbox_qx4)
    private CheckBox chbox_qx4;

    @ViewInject(R.id.chbox_qx5)
    private CheckBox chbox_qx5;

    @ViewInject(R.id.btn_ok)
    private Button btn_ok;

    private String[] items;

    private ArrayList<MapEntity> mList;
    private MapEntity mCurMapEntity;
    private String mCurType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server_permisson_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_permission_setting));
        llayout_select_user = (LinearLayout) findViewById(R.id.llayout_select_user);
        llayout_select_user.setOnClickListener(this);
        rlayout_qx1 = (RelativeLayout) findViewById(R.id.rlayout_qx1);
        rlayout_qx1.setOnClickListener(this);
        rlayout_qx2 = (RelativeLayout) findViewById(R.id.rlayout_qx2);
        rlayout_qx2.setOnClickListener(this);
        rlayout_qx3 = (RelativeLayout) findViewById(R.id.rlayout_qx3);
        rlayout_qx3.setOnClickListener(this);
        rlayout_qx4 = (RelativeLayout) findViewById(R.id.rlayout_qx4);
        rlayout_qx4.setOnClickListener(this);
        rlayout_qx5 = (RelativeLayout) findViewById(R.id.rlayout_qx5);
        rlayout_qx5.setOnClickListener(this);
        mList = new ArrayList<MapEntity>();
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null && bundle.containsKey("info")) {
                mCurMapEntity = bundle.getParcelable("info");
            }
        }
        setUserInfo();
    }

    private void setUserInfo() {
        if (mCurMapEntity != null && !mCurMapEntity.isEmpty()) {
            String name = mCurMapEntity.getString(AuditListResult.LOGIN_NAME);
            txt_select_user.setText(name);
            mCurType = mCurMapEntity.getString(AuditListResult.ROLE_ID);
            String qx1 = mCurMapEntity.getString(AuditListResult.QX1);
            if (TextUtils.equals("1", qx1)) {
                chbox_qx1.setChecked(true);
            } else {
                chbox_qx1.setChecked(false);
            }

            String qx2 = mCurMapEntity.getString(AuditListResult.QX2);
            if (TextUtils.equals("1", qx2)) {
                chbox_qx2.setChecked(true);
            } else {
                chbox_qx3.setChecked(false);
            }

            String qx3 = mCurMapEntity.getString(AuditListResult.QX3);
            if (TextUtils.equals("1", qx3)) {
                chbox_qx3.setChecked(true);
            } else {
                chbox_qx3.setChecked(false);
            }

            String qx4 = mCurMapEntity.getString(AuditListResult.QX4);
            if (TextUtils.equals("1", qx4)) {
                chbox_qx4.setChecked(true);
            } else {
                chbox_qx4.setChecked(false);
            }

            String qx5 = mCurMapEntity.getString(AuditListResult.QX5);
            if (TextUtils.equals("1", qx5)) {
                chbox_qx5.setChecked(true);
            } else {
                chbox_qx5.setChecked(false);
            }
        }
    }

    @Override
    protected void titlebarMethod(int id) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llayout_select_user:
                queryMembersByType();
                break;
            case R.id.rlayout_qx1:
                if (TextUtils.equals(mCurType, RoleType.CHECKER)) {
                    ToastHelper.getInstance().showShortMsg("审计员账号,不能设置该权限!");
                    return;
                }
                if (chbox_qx1.isChecked()) {
                    chbox_qx1.setChecked(false);
                } else {
                    chbox_qx1.setChecked(true);
                }
                break;
            case R.id.rlayout_qx2:
                if (chbox_qx2.isChecked()) {
                    chbox_qx2.setChecked(false);
                } else {
                    chbox_qx2.setChecked(true);
                }
                break;
            case R.id.rlayout_qx3:
                if (TextUtils.equals(mCurType, RoleType.CHECKER)) {
                    ToastHelper.getInstance().showShortMsg("审计员账号,不能设置该权限!");
                    return;
                }
                if (chbox_qx3.isChecked()) {
                    chbox_qx3.setChecked(false);
                } else {
                    chbox_qx3.setChecked(true);
                }
                break;
            case R.id.rlayout_qx4:
                if (TextUtils.equals(mCurType, RoleType.CHECKER)) {
                    ToastHelper.getInstance().showShortMsg("审计员账号,不能设置该权限!");
                    return;
                }
                if (chbox_qx4.isChecked()) {
                    chbox_qx4.setChecked(false);
                } else {
                    chbox_qx4.setChecked(true);
                }
                break;
            case R.id.rlayout_qx5:
                if (TextUtils.equals(mCurType, RoleType.CHECKER)) {
                    ToastHelper.getInstance().showShortMsg("审计员账号,不能设置该权限!");
                    return;
                }
                if (chbox_qx5.isChecked()) {
                    chbox_qx5.setChecked(false);
                } else {
                    chbox_qx5.setChecked(true);
                }
                break;
            default:
                break;
        }
    }

    private ProgressDialog mDialog;

    private void queryMembersByType() {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", imei);
        params.addBodyParameter("type1", RoleType.OPREATER);
        params.addBodyParameter("type2", RoleType.CHECKER);
        String url = ConfigProperties.SERVICE_URL + "queryMembersByType.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                mDialog = new ProgressDialog(ServerPermissionActivity.this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
                mList.clear();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ServerPermissionActivity.this != null && !ServerPermissionActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    parseQueryMembersByTypeResult(jsonObject);
                                    showListDialog();
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(ServerPermissionActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ServerPermissionActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (ServerPermissionActivity.this != null && !ServerPermissionActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ServerPermissionActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    private void parseQueryMembersByTypeResult(JSONObject jsonObject) throws JSONException {
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int length = jsonArray.length();
        items = new String[length];
        for (int i = 0; i < length; i++) {
            JSONObject jsonObject2 = jsonArray.getJSONObject(i);
            MapEntity mapEntity = new MapEntity();
            if (jsonObject2.has(AuditListResult.CHECKED_STR)) {
                mapEntity.setValue(AuditListResult.CHECKED, jsonObject2.getString(AuditListResult.CHECKED_STR));
            }
            if (jsonObject2.has(AuditListResult.CREATE_TIME_STR)) {
                mapEntity.setValue(AuditListResult.CREATE_TIME, jsonObject2.getString(AuditListResult.CREATE_TIME_STR));
            }
            mapEntity.setValue(AuditListResult.ERROR_TIMES, jsonObject2.getString(AuditListResult.ERROR_TIMES_STR));
            mapEntity.setValue(AuditListResult.FIRST_LOGIN, jsonObject2.getString(AuditListResult.FIRST_LOGIN_STR));
            mapEntity.setValue(AuditListResult.ID, jsonObject2.getString(AuditListResult.ID_STR));
            mapEntity.setValue(AuditListResult.LOCKED, jsonObject2.getString(AuditListResult.LOCKED_STR));
            mapEntity.setValue(AuditListResult.LOCKED_TIME, jsonObject2.getString(AuditListResult.LOCKED_TIME_STR));
            mapEntity.setValue(AuditListResult.LOGIN_NAME, jsonObject2.getString(AuditListResult.LOGIN_NAME_STR));
            mapEntity.setValue(AuditListResult.LOGIN_TIME, jsonObject2.getString(AuditListResult.LOCKED_TIME_STR));
            mapEntity.setValue(AuditListResult.MAC, jsonObject2.getString(AuditListResult.MAC_STR));
            mapEntity.setValue(AuditListResult.PASSWORD, jsonObject2.getString(AuditListResult.PASSWORD_STR));
            mapEntity.setValue(AuditListResult.QX1, jsonObject2.getString(AuditListResult.QX1_STR));
            mapEntity.setValue(AuditListResult.QX2, jsonObject2.getString(AuditListResult.QX2_STR));
            mapEntity.setValue(AuditListResult.QX3, jsonObject2.getString(AuditListResult.QX3_STR));
            mapEntity.setValue(AuditListResult.QX4, jsonObject2.getString(AuditListResult.QX4_STR));
            mapEntity.setValue(AuditListResult.QX5, jsonObject2.getString(AuditListResult.QX5_STR));
            mapEntity.setValue(AuditListResult.ROLE_ID, jsonObject2.getString(AuditListResult.ROLE_ID_STR));
            mapEntity.setValue(AuditListResult.UUID, jsonObject2.getString(AuditListResult.UUID_STR));
            mList.add(mapEntity);
            items[i] = mapEntity.getString(AuditListResult.LOGIN_NAME);
        }
    }

    private void showListDialog() {
        if (items == null || items.length == 0) {
            ToastHelper.getInstance().showShortMsg("没有可以操作的用户!");
            return;
        }
        String title = "请选择用户名";
        DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mCurMapEntity = mList.get(which);
                setUserInfo();
            }
        };
        new ViewHelper(ServerPermissionActivity.this).showListDialog(title, items,
                clickListener);
    }

    @OnClick(R.id.btn_ok)
    public void btn_ok(View view) {
        if (mCurMapEntity == null || mCurMapEntity.isEmpty()) {
            ToastHelper.getInstance().showShortMsg("请选择用户!");
            return;
        }
        String username = mCurMapEntity.getString(AuditListResult.LOGIN_NAME);
        if (TextUtils.isEmpty(username)) {
            ToastHelper.getInstance().showShortMsg("请选择用户!");
            return;
        }
        shezhiByAdmin(username);
    }

    private void shezhiByAdmin(String username) {
        boolean ic_checked_qx1 = chbox_qx1.isChecked();
        String qx1 = "";
        if (ic_checked_qx1) {
            qx1 = "1";
        }

        boolean ic_checked_qx2 = chbox_qx2.isChecked();
        String qx2 = "";
        if (ic_checked_qx2) {
            qx2 = "1";
        }

        boolean ic_checked_qx3 = chbox_qx3.isChecked();
        String qx3 = "";
        if (ic_checked_qx3) {
            qx3 = "1";
        }

        boolean ic_checked_qx4 = chbox_qx4.isChecked();
        String qx4 = "";
        if (ic_checked_qx4) {
            qx4 = "1";
        }

        boolean ic_checked_qx5 = chbox_qx5.isChecked();
        String qx5 = "";
        if (ic_checked_qx5) {
            qx5 = "1";
        }
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", imei);
        params.addBodyParameter("username", username);
        if (!TextUtils.isEmpty(qx1)) {
            params.addBodyParameter("qx1", qx1);
        }
        if (!TextUtils.isEmpty(qx2)) {
            params.addBodyParameter("qx2", qx2);
        }
        if (!TextUtils.isEmpty(qx3)) {
            params.addBodyParameter("qx3", qx3);
        }
        if (!TextUtils.isEmpty(qx4)) {
            params.addBodyParameter("qx4", qx4);
        }
        if (!TextUtils.isEmpty(qx5)) {
            params.addBodyParameter("qx5", qx5);
        }
        String url = ConfigProperties.SERVICE_URL + "shezhiByAdmin.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                mDialog = new ProgressDialog(ServerPermissionActivity.this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ServerPermissionActivity.this != null && !ServerPermissionActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                String msg = jsonObject.getString("message");
                                if (success) {
                                    ToastHelper.getInstance().showShortMsg(msg);
                                } else {
                                    new ViewHelper(ServerPermissionActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ServerPermissionActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (ServerPermissionActivity.this != null && !ServerPermissionActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ServerPermissionActivity.this).showErrorDialog(error);
                }
            }
        });

    }
}
